<?php

/**
 * di容器
 */

namespace wtfphp;

class di {
    private static $_container = array();

    /**
     * 设置
     * @param $name     名称
     * @param $value    闭包
     */
    static function set($name, \Closure $value) {
        self::$_container[$name] = $value;
    }

    /**
     * 获取
     */
    static function get($name) {
        if (isset(self::$_container[$name])) {
            if (self::$_container[$name] instanceof \Closure) {
                $closure = self::$_container[$name];
                self::$_container[$name] = $closure();
            }

            return self::$_container[$name];
        }

        return null;
    }

    /**
     * 是否已设置
     * @param $name
     * @return bool
     */
    static function isHas($name) {
        if (isset(self::$_container[$name])) {
            return true;
        } else {
            return false;
        }
    }
}